#! /usr/bin/env python
#coding=utf-8

import uocbase
import time

class Uoc(uocbase.UocBase):
    "UOC_add"
    uoclevel = 'normal'
    def direct_parse(self):
        "直接型命令分析"
        if self.uocparams[0] == 't':
            adddd = {'r_datetime': 1227163361, 'r_type': 2, 
                     'r_description': '1','r_amount': 1.0,
                     'b_name': '1', 'r_level': 1}
            self.aoclist.append(['add', adddd])

    def interact_parse(self):
        "交互性命令分析"
        print '增加新条目'
        rdatetime = None
        while not rdatetime:
            st = self.get_input('请输入时间（默认为当前时间）')
            if not st:
                return None
            if st == 'default':
                rdatetime = int(time.time())
                break
            else:
                rdatetime = uocbase.field_validate(st, 'datetime')
                if rdatetime:
                    rdatetime = int(rdatetime)
                    break
                else:
                    print '格式输入错误'
        print time.ctime(rdatetime)
        
        rtype = None
        while not rtype:
            st = self.get_input('请输入类型（1:收入;2:支出）默认为支出')
            if not st:
                return None
            if st == 'default':
                rtype = 2
                break
            else:
                rtype = uocbase.field_validate(st, 'int')
                if rtype == 1 or rtype == 2:
                    break
                else:
                    print '格式输入错误'
        print rtype
        
        ramount = None
        while not ramount:
            st = self.get_input('请输入金额（单位为元）')
            if not st:
                return None
            if st == 'default':
                continue
            else:
                ramount = uocbase.field_validate(st, 'double')
                if ramount:
                    break
                else:
                    print '格式输入错误'
        print ramount
        
        bname = None
        while not bname:
            st = self.get_input('请输入收/支名(len<20)')
            if not st:
                return None
            if st == 'default':
                continue
            else:
                bname = uocbase.field_validate(st, 'string', 20)
                if bname:
                    break
                else:
                    print '格式输入错误'
        print bname
        
        rdescription = None
        while not rdescription:
            st = self.get_input('请输入该记录的描述(len<1000)，非必须')
            if not st:
                return None
            if st == 'default':
                rdescription = ''
                break
            else:
                rdescription = uocbase.field_validate(st, 'string', 1000)
                if rdescription:
                    break
                else:
                    print '格式输入错误'
        print rdescription
        
        rlevel = None
        while not rlevel:
            st = self.get_input('请输入记录级别（1:计划内;2:计划外;3:未达到计划;4:超出计划;5:未预料）')
            if not st:
                return None
            if st == 'default':
                continue
            else:
                rlevel = uocbase.field_validate(st, 'int')
                if rlevel > 0 and rlevel < 5:
                    break
                else:
                    print '格式输入错误'
        print rlevel
        
        # addd means add data dict ^_^
        adddd = {
            'r_datetime':rdatetime,
            'r_type':rtype,
            'r_amount':ramount,
            'b_name':bname,
            'r_description':rdescription,
            'r_level':rlevel
            }
        
        # 输出输入参数，可以写的好看一点
        print adddd
        
        yn = None
        while not yn:
            st = self.get_input('确定要添加该条记录么？(y/n)')
            if not st:
                return None
            if st == 'default':
                break
            else:
                yn = uocbase.field_validate(st, 'yn')
                if yn:
                    break
                else:
                    print '格式输入错误'
        if yn == 'y':
            print '确定插入操作'
            self.aoclist.append(['add', adddd])
            print 'hey'
            print self.aoclist
            return [['add', adddd]]
        else:
            print '取消'
            return None
